<template>
  <div class="content">
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="名字" prop="name" required>
        <el-input v-model="ruleForm.name" placeholder="请输入名字"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age" required>
        <el-input
          type="number"
          v-model="ruleForm.age"
          placeholder="请输入年龄"
        ></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="account" required>
        <el-input
          type="number"
          v-model="ruleForm.account"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex" required>
        <el-select v-model="ruleForm.sex" placeholder="请选择性别">
          <el-option label="男" value="0"></el-option>
          <el-option label="女" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" required>
        <el-col :span="11">
          <el-form-item prop="date1">
            <el-date-picker
              v-model="ruleForm.date1"
              type="date"
              placeholder="请选择日期"
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-form-item prop="date2">
            <el-time-picker
              v-model="ruleForm.date2"
              placeholder="请选择时间"
              style="width: 100%"
            ></el-time-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="爱好" prop="hobby">
        <el-checkbox-group v-model="ruleForm.hobby">
          <el-checkbox
            v-for="(item, index) in hobbyList"
            :key="index"
            :label="item.id"
            >{{ item.name }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="状态" prop="resource">
        <el-radio-group v-model="ruleForm.resource">
          <el-radio label="0">启用</el-radio>
          <el-radio label="1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="ruleForm.desc" type="textarea"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        sex: "",
        age: "",
        account: "",
        date1: "",
        date2: "",
        hobby: [],
        resource: "1",
        desc: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入名字",
            trigger: "blur",
          },
          {
            min: 3,
            max: 6,
            message: "长度为3到6位",
            trigger: "blur",
          },
        ],
        sex: [{ required: true, message: "请输入性别", trigger: "blur" }],
        age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        date1: [{ required: true, message: "请输入日期", trigger: "blur" }],
        date2: [{ required: true, message: "请输入时间", trigger: "blur" }],
      },
      hobbyList: [
        { id: "1", name: "唱" },
        { id: "2", name: "跳" },
        { id: "3", name: "rap" },
        { id: "4", name: "运动" },
        { id: "5", name: "阅读" },
        { id: "6", name: "旅游" },
      ],
    };
  },
  methods: {
    submitForm(NameForm) {
      this.$refs[NameForm].validate((valid) => {
        if (valid) {
          console.log(this.ruleForm);
          alert("提交成功");
        } else {
          alert("失败");
          return false;
        }
      });
    },
    resetForm(NameForm) {
      this.ruleForm = {};
    },
  },
};
</script>

<style lang="less" scoped>
.content {
  width: 100%;
  height: 100%;
  background-color: #fff;
  padding-top: 20px;
}
:deep(.el-input__inner) {
  width: 300px;
}
:deep(.el-textarea__inner) {
  width: 300px !important;
}
</style>